from Spider.util.Base import BaseMiddleware
from Spider.util.UserAgentPool import MyUserAgent
from Spider.settings import HEADERS, STATUS_CODE


class MyMiddleware(BaseMiddleware):
    def process_request(self, request):
        request.headers = HEADERS
        request.headers['User-Agent'] = MyUserAgent.random()
        return request

    def process_response(self, request, response):
        city = request.meta.get('city')
        page = request.meta.get('page')
        fail_time = request.fail_time
        if response.status in STATUS_CODE:
            result = response.json()
            # 响应状态码为200  但是响应数据是错误数据
            if result is None:
                print(f'城市:{city}  页码:{page}  失败次数:{fail_time}  解析页面为错误页面，请求对象的失败次数+1')
                return request
            if result.get('code', None) == 406 and result.get('msg', None):
                print(f'城市:{city}  页码:{page}  失败次数:{fail_time}  解析页面为错误页面，请求对象的失败次数+1')
                return request
            return response
        else:
            print(f'城市:{city}  页码:{page}  失败次数:{fail_time}  响应状态码不合法，请求对象的失败次数+1')
            return request
